const app = getApp()
var netUtil = require("../../utils/netUtil.js"); //require引入

Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '团队修改信息页', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
    userInfo: wx.getStorageSync("userInfo")
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync("userInfo"),
      province:wx.getStorageSync("province"),
      city:wx.getStorageSync("city"),
      type: options.type//type===new表示新增  edit表示修改
    })
    if (this.data.type==='edit'){//修改
      let data = JSON.parse(options.data)
      this.setData({
        teamDatas: JSON.parse(options.data),
        name:data.name,
        mobile: data.mobile,
        // commission_percent: data.commission_percent,
        team_tag_id: data.team_tag_id
      })
    } else if (this.data.type=="sh"){//审核
      this.setData({
        id: options.id
      })
    }else{
      this.setData({
        tel: options.tel
      })
    }
    this.getTagList();
  },
  getName(e){
    this.setData({
      name:e.detail.value
    })
  },
  getMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // getPercent(e) {
  //   this.setData({
  //     commission_percent: e.detail.value
  //   })
  // },
  getTagId(e) {
    this.setData({
      team_tag_id: e.detail.value
    })
  },
  // 获取团队标签列表
  getTagList(){
    netUtil.postData('/team-tag-list','', (datas) => {
      if (this.data.type === 'edit'){
        datas.data.lists.forEach((res,index) => {
          if (res.name === this.data.teamDatas.team_tag_name){           
            this.setData({
              tagList: datas.data.lists,
              tagIndex:index
            })
          }        
        })
      }else{
        this.setData({
          tagList: datas.data.lists,
          tagIndex: 0
        })
      }
      

      
    })
  },
  tagChange(e){
    var that = this;   
    this.setData({
      tagIndex: e.detail.value        
    })
    
  },
  // 提交
  submit(){
    if (this.verification()===false) return
    
    let params={}
    var url="";
    if (this.data.type === 'edit'){//修改
       params = {
         id: this.data.teamDatas.id,
        token: this.data.userInfo.token,
        name: this.data.name,
        mobile: this.data.mobile,
        // commission_percent: this.data.commission_percent,
         team_tag_id: this.data.tagList[this.data.tagIndex].id,
        province: this.data.province,
        city: this.data.city
      }
      url ="/submit-team"
    } else if (this.data.type == "sh"){//审核
      params = {
        token: this.data.userInfo.token,
        // name: this.data.name,
        // mobile: this.data.mobile,
        // team_tag_id: this.data.tagList[this.data.tagIndex].id,
        // province: this.data.province,
        // city: this.data.city,
        id:this.data.id
      }
      url = "/published-team"
    }else{
       params = {
        token: this.data.userInfo.token,
        name: this.data.name,
        mobile: this.data.mobile,
        // commission_percent: this.data.commission_percent,
         team_tag_id: this.data.tagList[this.data.tagIndex].id,
        province: this.data.province,
        city: this.data.city,
         team_mobile: this.data.tel
      }
      url = "/submit-team"
    }
    console.log(params)
    netUtil.postData(url,params, (datas) => {
      if (datas.code===0){
        wx.showToast({
          title: '创建成功',
        })
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          })
        },1000)
      }else{
        wx.showToast({
          title: datas.message,
          icon:'none'
        })
      }
    })
  },
  // 验证
  verification(){
  
    if (this.data.name == "" || this.data.name ==undefined){
      wx.showToast({
        title: '请输入团队名称',
        icon:'none'
      })
      return false
    } else if (this.data.mobile == "" || this.data.mobile == undefined){
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none'
      })
      return false
    } 
    // else if (this.data.commission_percent == undefined ) {
      
    //   wx.showToast({
    //     title: '请输入佣金比例',
    //     icon: 'none'
    //   })
    //   return false
    // } 
    return true
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})